package ee.homework.seventh;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class AdjacencyMatrix {

	private String[] files = new String[] { "G1.txt", "G2.txt", "G3.txt" };

	public static void main(String[] args) {
		AdjacencyMatrix graph = new AdjacencyMatrix();
		graph.start();
	}

	private void start() {
		for (int i = 0; i < files.length; i++) {
			int[][] matrix = readFileToMatrix("Data\\" + files[i]);
			System.out.println("Graph " + files[i].split("\\.")[0]);
			System.out.println("Original graph");
			int sum = sumEdges(matrix);
			System.out.println("Edges: " + sum);
			int[][] newMatrix = createUndirectedGraph(matrix);
			System.out.println("Symmetrically connectected graph");
			sum = sumEdges(newMatrix);
			System.out.println("Edges: " + sum);
			System.out.println();
		}
	}

	private int[][] readFileToMatrix(String file) {
		int[][] matrix = null;
		try {
			BufferedReader reader = new BufferedReader(new FileReader(new File(
					file)));

			String line = null;
			int min = -1;
			int max = -1;
			List<String[]> list = new ArrayList<String[]>();
			while ((line = reader.readLine()) != null) {
				String[] splitLine = line.split(">");
				list.add(splitLine);
				int source = Integer.parseInt(splitLine[0].trim());
				int destination = Integer.parseInt(splitLine[1].trim());
				if (min == -1 || min > source)
					min = source;
				if (min > destination)
					min = destination;

				if (max == -1 || max < source)
					max = source;
				if (max < destination)
					max = destination;
			}
			int size = max - min + 1;
			matrix = new int[size][size];
			int row;
			int col;
			for (String[] strings : list) {
				row = Integer.parseInt(strings[0].trim());
				col = Integer.parseInt(strings[1].trim());
				matrix[row][col] = 1;
			}
		} catch (FileNotFoundException e) {
			System.out.println("File " + file + " does not exist");
		} catch (IOException e) {
			System.out.println("I/O exception");
			e.printStackTrace();
		}
		return matrix;
	}

	public Stack<Integer> breadthFirstSearch(int[][] matrix, int start) {
		Stack<Integer> queue = new Stack<Integer>();
		Stack<Integer> visited = new Stack<Integer>();

		queue.push(start);
		visited.push(start);
		while (!queue.isEmpty()) {
			int node = queue.pop();
			for (int i = 0; i < matrix.length; i++) {
				if (matrix[node][i] == 1 && !visited.contains(i)) {
					queue.push(i);
					visited.push(i);
				}
			}
		}
		return visited;
	}

	private int sumEdges(int[][] matrix) {
		int sum = 0;
		for (int i = 0; i < matrix.length; i++) {
			for (int j = 0; j < matrix[0].length; j++) {
				sum += matrix[i][j];
			}
		}
		return sum;
	}

	private int[][] createUndirectedGraph(int[][] matrix) {
		int[][] tempMatrix = matrix;
		for (int i = 0; i < tempMatrix.length; i++) {
			for (int j = 0; j < tempMatrix[0].length; j++) {
				if (tempMatrix[i][j] == 1) {
					tempMatrix[j][i] = 1;
				}
			}
		}
		return tempMatrix;
	}
}
